/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.jprvyd.gui.model;

import cz.muni.fi.pb138.jprvyd.Contact;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Jan
 */
public class Contact_TableModel extends AbstractTableModel {
    
    List<Contact> contacts;
    private static int COLUMN_COUNT = 3;

    public Contact_TableModel(List<Contact> contacts) {
        this.contacts = contacts;
    }   

    @Override
    public int getRowCount() {
        return contacts.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMN_COUNT;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Contact contact = contacts.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return contact.getFirstName();
            case 1:
                return contact.getSurname();
            case 2:
                return contact.getIco();
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }
    
    public void addContact(Contact contact) {
        contacts.add(contact);
        int lastRow = getRowCount() - 1;
        fireTableRowsInserted(lastRow, lastRow);
    }

    public void editContact(Contact contact) {
        int index = contacts.indexOf(contact);
        contacts.set(index, contact);
        fireTableRowsUpdated(index, index);
    }

    public void deleteContact(int row) {
        contacts.remove(contacts.get(row));
        fireTableRowsDeleted(row, row);
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "First name";
            case 1:
                return "Surname";
            case 2:
                return "ICO";
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return String.class;
            case 2:
                return Integer.class;
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }
    
}
